package com.bawei.demo_widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @author yaotianxue
 * @description
 * @date 2025/5/15
 */
public class MyView extends View {
    private Paint mPaint; //构造中创建画笔，千万不要在onDraw，否则产生内存抖动
    private int mColor;//颜色
    private int mRadius; //半径
    private int mSweepAngle; //扫描的弧度
    private ValueAnimator mAnimator;
    public MyView(Context context) {
        this(context,  null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        mColor = typedArray.getColor(R.styleable.MyView_color, Color.BLUE);
        mRadius = typedArray.getInt(R.styleable.MyView_radius,0);
        initPaint();
    }
    private void initPaint(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true); //抗锯齿
        mPaint.setDither(true);//防抖动
        mPaint.setColor(mColor);
        mPaint.setStyle(Paint.Style.STROKE);//空心
        mPaint.setStrokeWidth(5); //dp转px
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.d("MyView", "onMeasure: ");
        //获得测模式和大小
        int width_mode = MeasureSpec.getMode(widthMeasureSpec);
        int width_size = MeasureSpec.getSize(widthMeasureSpec);
        Log.d("MyViewGroup", "宽度测量模式: "+width_mode);
        Log.d("MyViewGroup", "宽度测量大小: "+width_size);
        int height_mode = MeasureSpec.getMode(heightMeasureSpec);
        int height_size = MeasureSpec.getSize(heightMeasureSpec);
        Log.d("MyViewGroup", "高度测量模式: "+height_mode);
        Log.d("MyViewGroup", "高度测量大小: "+height_size);
        //wrap_content处理
        if ( width_mode == MeasureSpec.AT_MOST){
            width_size = mRadius * 2;
        }
        if (height_mode == MeasureSpec.AT_MOST){
            height_size = mRadius * 2;
        }
        setMeasuredDimension(width_size,height_size);
    }

    /**
     * canvas 画布
     * paint 画笔
     * 注意：千万不要在onDraw方法中创建对象，频繁创建销毁，会频繁GC，导致内存抖动
     */
    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        Log.d("MyView", "onDraw: ");
//        canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,mRadius,mPaint);//圆形
        //左 上 右 下
        canvas.drawArc(getMeasuredWidth()/2-mRadius,getMeasuredHeight()/2-mRadius,getMeasuredWidth()/2+mRadius,getMeasuredHeight()/2+mRadius,0,mSweepAngle,false,mPaint);
    }


    public void start(){
        if (mAnimator == null){
            mAnimator = ValueAnimator.ofInt(0,360);
            mAnimator.setDuration(5*1000);
            mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                    mSweepAngle = (int) animation.getAnimatedValue();
                    invalidate(); //主线程刷新界面,调用onDraw方法
//                    postInvalidate(); //子线程通知刷新界面，借助handler，刷新界面还是在主线程中
                }
            });
        }
        if (!mAnimator.isRunning()){
            mAnimator.start();
        }
    }
    public void cancel(){
        if (mAnimator != null){
            mAnimator.cancel();
        }
    }



}
